package com.android.clinic.app.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.clinic.app.R;

/**
 * Created by cheng on 2018/2/7.
 */
public class CustomSheetSexDialog {

    private Context context;
    private Dialog dialog;
    private Display display;
    private LinearLayout layCanel;
    private LinearLayout laySex;
    private OnSheetItemClickListener itemClickListener;

    public CustomSheetSexDialog setOnSheetItemClickListener(
            OnSheetItemClickListener listener) {
        this.itemClickListener = listener;
        return this;
    }

    public CustomSheetSexDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public CustomSheetSexDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_sex_layout, null);

        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());

        // 获取自定义Dialog布局中的控件
        laySex = (LinearLayout) view.findViewById(R.id.layout_sex);
        layCanel = (LinearLayout) view.findViewById(R.id.layout_cancel);

        layCanel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        int count = laySex.getChildCount();
        for (int i = 0; i < count; i++) {
            if (laySex.getChildAt(i) instanceof TextView) {
                TextView tv = (TextView) laySex.getChildAt(i);
                final int which = Integer
                        .valueOf(tv.getTag().toString().trim());
                tv.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (itemClickListener != null) {
                            itemClickListener.onClick(which);
                        }
                        dialog.dismiss();
                    }
                });

            }
        }

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    public CustomSheetSexDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public CustomSheetSexDialog setCanceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public void show() {
        dialog.show();
    }

    public interface OnSheetItemClickListener {
        void onClick(int which);
    }

}
